from django.http import HttpResponseRedirect
from django.shortcuts import get_object_or_404, render
from django.urls import reverse
from django.views import generic
from django.utils import timezone
from django.conf import settings
from django.core.paginator import Paginator
from hoedown import Markdown, HtmlRenderer
import hashlib
import requests

from .models import Problem, User

s=requests.Session()

def hashpwd(pwd):
    if pwd is None:
        return hashlib.md5((settings.SECRET_KEY).encode(encoding='UTF-8')).hexdigest()
    return hashlib.md5((pwd+settings.SECRET_KEY).encode(encoding='UTF-8')).hexdigest()

def getlen(s):
    if s is None or len(s) == 0:
        return 0
    else:
        return len(s)

def global_setting(request):
    return {
        "OJ_NAME": settings.OJ_NAME,
    }

def index(request):
    return render(request, 'index/index.html')

def problemList(request):
    latest_problem_list = Problem.objects.order_by('id')[:5]
    problem_pages = Paginator(latest_problem_list, 25)
    now_page = request.GET.get('page')
    if now_page is None:
        now_page = 1
    now_page = int(now_page)
    if now_page > problem_pages.num_pages or now_page <= 0:
        now_page = 1
    problem_list = problem_pages.page(now_page)
    context = {'problems_list': problem_list}
    return render(request, 'problems/index.html', context)

def problemView(request, id):
    problem = get_object_or_404(Problem, pk=id)
    rndr = HtmlRenderer()
    md = Markdown(rndr)
    problem.problem_info = md.render(problem.problem_info)
    return render(request, 'problems/detail.html', {'problem': problem})

def problemSubmit(request, id):
    problem = get_object_or_404(Problem, pk=id)
    rndr = HtmlRenderer()
    md = Markdown(rndr)
    problem.problem_info = md.render(problem.problem_info)
    return render(request, 'problems/submit.html', {'problem': problem})

def login(request):
    return render(request, 'users/login.html')

def reg(request):
    return render(request, 'users/reg.html')

def checklogin(request):
    retcode = 1
    retmsg = "未知错误"
    getuser = request.POST.get('username')
    getpasswd = hashpwd(request.POST.get('password'))
    findUser = User.objects.filter(user_id = getuser, password = getpasswd);
    if len(findUser)==0 :
        retmsg = "用户名或者密码错误"
        return render(request, 'jcode.html', {'code': retcode, 'message': retmsg})
    request.session['user_id'] = getuser
    return render(request, 'jcode.html', {'code': 0, 'message': "登录成功"})
    return render(request, 'jcode.html', {'code': retcode, 'message': retmsg})

def checkreg(request):
    retcode = 1
    retmsg  = "未知错误"
    getuser = request.POST.get('username')
    getpasswd = request.POST.get('password')
    getrptpasswd = request.POST.get('rptpassword')
    if getpasswd != getrptpasswd:
        retmsg = "密码不一致"
        return render(request, 'jcode.html', {'code': retcode, 'message': retmsg})
    strlen = getlen(getpasswd)
    if strlen < 6:
        retmsg = "密码至少6位"
        return render(request, 'jcode.html', {'code': retcode, 'message': retmsg})
    if strlen > 32:
        retmsg = "密码最多32位"
        return render(request, 'jcode.html', {'code': retcode, 'message': retmsg})
    getpasswd = hashpwd(getpasswd)
    strlen = getlen(getuser)
    if strlen > 100:
        retmsg = "用户名太长了"
        return render(request, 'jcode.html', {'code': retcode, 'message': retmsg})
    if strlen <= 2:
        retmsg = "用户名太短了"
        return render(request, 'jcode.html', {'code': retcode, 'message': retmsg})
    getschool = request.POST.get('school')
    strlen = getlen(getschool)
    if strlen > 100:
        retmsg = "学校名称太长了"
        return render(request, 'jcode.html', {'code': retcode, 'message': retmsg})
    if strlen <= 2:
        retmsg = "学校名称太短了"
        return render(request, 'jcode.html', {'code': retcode, 'message': retmsg})
    getemail = request.POST.get('email')
    strlen = getlen(getemail)
    if strlen > 100:
        retmsg = "邮箱太长了"
        return render(request, 'jcode.html', {'code': retcode, 'message': retmsg})
    if strlen <= 2:
        retmsg = "邮箱太短了"
        return render(request, 'jcode.html', {'code': retcode, 'message': retmsg})
    getqq = request.POST.get('qq')
    strlen = getlen(getqq)
    if strlen > 30:
        retmsg = "QQ太长了"
        return render(request, 'jcode.html', {'code': retcode, 'message': retmsg})
    if strlen <= 2:
        retmsg = "QQ太短了"
        return render(request, 'jcode.html', {'code': retcode, 'message': retmsg})
    findUser = User.objects.filter(user_id = getuser);
    if len(findUser) > 0:
        retmsg = "用户名已经存在了"
        return render(request, 'jcode.html', {'code': retcode, 'message': retmsg})
    nUser = User(user_id = getuser, password = getpasswd, email = getemail, school = getschool, qq = getqq);
    nUser.save();
    return render(request, 'jcode.html', {'code': 0, 'message': "注册成功,请登录"})
    return render(request, 'jcode.html', {'code': retcode, 'message': retmsg})

def userView(request, uid):
    userinfo = get_object_or_404(User, user_id=uid)
    return render(request, 'users/detail.html', {'userinfo': userinfo})

def userRank(request, mod):
    modshow = "未知"
    if mod == "score":
        latest_user_list = User.objects.order_by('-score')[:5]
        modshow = "积分"
    elif mod == "hb":
        latest_user_list = User.objects.order_by('-hb')[:5]
        modshow = "壕币"
    else:
        latest_user_list = User.objects.order_by('-id')[:5]
        modshow = "id"
    context = {'users_list': latest_user_list, 'modshow': modshow }
    return render(request, 'users/rank.html', context)